<template>
	<view class="address">
		<view class="main">
			<view class="empty" v-if="addressList.length == 0">
				<view class="img">
				</view>
				<view class="txt">您还没有收货地址</view>
			</view>
			<view class="addressItem" v-for="(item, index) in addressList" :key="item.addrId">
				<view class="addressItem_top" v-if="stat == 1" @click="saveAddress(item)">
					<view class="addressItem_top_left">
						<view class="addr_phone">
							{{ item.receiver }}&emsp;
							{{ item.mobile }}
						</view>
						<view class="addr">
							{{ `${item.province} ${item.city} ${item.area} ${item.addr}` }}
						</view>
					</view>
				</view>

				<view class="addressItem_top" v-else>
					<view class="addressItem_top_left">
						<view class="addr_phone">
							{{ item.receiver }}&emsp;
							{{ item.mobile }}
						</view>
						<view class="addr">
							{{ `${item.province} ${item.city} ${item.area} ${item.addr}` }}
						</view>
					</view>
					<view class="addressItem_top_right" @click="editAddress(item)">
						编辑
					</view>
				</view>

				<view class="addressItem_buttom">
					<label>
						<radio :checked="index === checkInd ? true : false" :name="item.addrId" color="#eb2444"
							@click="defaults(item.addrId, index)" />
						设为默认地址
					</label>
				</view>
			</view>
		</view>
		<view class="footer" @click="addAddress">
			<view>新增收货地址</view>
		</view>
	</view>
</template>

<script>
	import {
		get_address,
		set_defaultAddr
	} from "../../../services/address.js";
	export default {
		data() {
			return {
				addressList: [],
				checkInd: 0,
				addrId: uni.getStorageSync("addrId") || "",
				stat: 0,
			};
		},
		async onLoad(options) {
			this.stat = options.stat;
			console.log(options, "ididididiidididiidiidididididi");
			await this.init();
			this.onLoading();
		},
		async onShow() {
			await this.init();
		},
		methods: {
			//加载动画
			onLoading() {
				uni.showLoading({
					title: "加载中",
					duration: 1000,
				});
			},
			// 收货地址数据
			async init() {
				let res = await get_address({
					url: "/p/address/list",
				});
				this.addressList = res;
				console.log(res, "收货地址获取数据");
			},
			// 跳转新增收货地址页面
			addAddress() {
				uni.navigateTo({
					url: '/pages/component/editAddress/editAddress?flag=1',
				});
			},
			// 跳转编辑收货地址页面
			editAddress(item) {
				const editObj = JSON.stringify(item);
				uni.navigateTo({
					url: `/pages/component/editAddress/editAddress?flag=2&editObj=${editObj}`,
					// query: { flag: 2, editObj },
				});
				console.log(editObj, "传参")
			},
			//设置默认地址
			async defaults(option, indexs) {
				await set_defaultAddr({
					url: `/p/address/defaultAddr/${option}`,
					method: "put",
				});
				this.checkInd = indexs;
				this.addrId = option.id;
			},
		},
	};
</script>

<style scoped>
	@import "./index.css";
</style>
